package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.person.common.BaseContext;
import com.person.common.CustomException;
import com.person.dto.DishDto;
import com.person.mapper.DishFlavorMapper;
import com.person.pojo.DishFlavor;
import com.person.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public void addFlavor(DishDto dto) {
        Long dishId = dto.getId();
        //设置口味表信息，添加进表dish_flavor
        List<DishFlavor> flavors = dto.getFlavors();


        //使用lambda表达式的方式
//        List<DishFlavor> flavors = flavors.stream().map((flavor) -> {
//            flavor.setDishId(dishId);
//            flavor.setId(IdUtil.getSnowflakeNextId());
//            flavor.setUpdateTime(LocalDateTime.now());
//            flavor.setCreateTime(LocalDateTime.now());
//            flavor.setCreateUser(BaseContext.getCurrentId());
//            flavor.setUpdateUser(BaseContext.getCurrentId());
//            return flavor;
//        }).collect(Collectors.toList());


        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dto.getId());
            flavor.setId(IdUtil.getSnowflakeNextId());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setCreateUser(BaseContext.getCurrentId());
            flavor.setUpdateUser(BaseContext.getCurrentId());
        }
        log.info("添加口味信息{}",flavors);

        dishFlavorMapper.add(flavors);
    }

    @Override

    public void update(DishDto dishDto) {
        //获取菜品id
        Long id = dishDto.getId();
        //先移除口味
        dishFlavorMapper.remove(id);


        //得到口味属性
        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors==null){
            throw new CustomException("请添加口味信息");
        }
        //判断是否有 口味信息
        if (flavors!=null){
            for (DishFlavor flavor : flavors) {
                flavor.setId(IdUtil.getSnowflakeNextId());
                flavor.setDishId(id);
                flavor.setCreateTime(LocalDateTime.now());
                flavor.setCreateUser(BaseContext.getCurrentId());
                flavor.setUpdateTime(LocalDateTime.now());
                flavor.setUpdateUser(BaseContext.getCurrentId());
            }
            log.info("修改后的口味：{}",flavors);
            dishFlavorMapper.add(flavors);
        }


    }

    @Override
    public List<DishFlavor> getFlavor(Long id) {
        List<DishFlavor> dishFlavors = dishFlavorMapper.select(id);
        return dishFlavors;
    }
}
